package com.easyaop.test.core.pointcut;

import com.easyaop.test.core.annotation.Aop;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author lipan
 * @since 2025-01-24
 */
public class AnnotationPointcut extends Pointcut implements Iterable<MethodPointcut> {
    private final String className;

    private final List<MethodPointcut> methodPointcuts;

    public AnnotationPointcut(Class<?> target) {
        this.className = target.getName();
        this.methodPointcuts = findAnnotation(target);
    }

    public static List<MethodPointcut> findAnnotation(Class<?> target) {
        List<MethodPointcut> methodPointcuts = new LinkedList<>();
        if (target.isAnnotationPresent(Aop.class)) {
            for (Method method : target.getDeclaredMethods()) {
                methodPointcuts.add(MethodPointcut.of(method));
            }
            return methodPointcuts;
        }
        for (Method method : target.getDeclaredMethods()) {
            if (method.isAnnotationPresent(Aop.class)) {
                methodPointcuts.add(MethodPointcut.of(method));
            }
        }
        return methodPointcuts;
    }

    @Override
    public Iterator<MethodPointcut> iterator() {
        return methodPointcuts.iterator();
    }
}
